package com.example.mingyue.controller;

import ch.qos.logback.core.util.StringUtil;
import com.example.mingyue.common.Result;
import com.example.mingyue.model.dto.PostDto;
import com.example.mingyue.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 帖子发布
 */
@RestController
@RequestMapping("/post")
@Slf4j
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 发布帖子
     * @return
     */
    @PostMapping("add")
    public Result addPost(@RequestBody PostDto postDto){
        if(postDto == null){
            return Result.error("帖子数据为空");
        }
        if(StringUtil.isNullOrEmpty(postDto.getTitle()) || StringUtil.isNullOrEmpty(postDto.getContent())){
            return Result.error("请输入标题或内容");
        }

        postService.addPost(postDto);
        return Result.success();
    }
    /**
     * 删除帖子
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deletePost(@PathVariable Integer id){
        postService.deletePost(id);
        return Result.success();
    }

}
